package com.ss.android.common.app;

import android.app.Application;
import android.content.Intent;
import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelStore;
import androidx.lifecycle.ViewModelStoreOwner;
import com.ss.android.common.AppContext;


/**
 */
public abstract class AbsApplication extends Application  implements ViewModelStoreOwner {

    protected static AppContext sAppContext;
    protected volatile static Application application;

    public static String sManifestVersion;
    public static int sManifestVersionCode;
    public static int sUpdateVersionCode = -1;
    public static String sChannel = "";
    public static boolean sEventLifeCyclerInited = false;

    private ViewModelStore mAppViewModelStore;
    public static volatile boolean isApplicationOnCreate = false;
    public static volatile boolean isApplicationOnCreateFinish = false;
    public static final Object sFlippedLock = new Object();
    @Override
    public void startActivity(Intent intent) {
        if((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) == 0){
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        super.startActivity(intent);
    }


    @Override
    public void onCreate() {
        super.onCreate();
        isApplicationOnCreate = true;
        mAppViewModelStore = new ViewModelStore();
    }


    @NonNull
    @Override
    public ViewModelStore getViewModelStore() {
        return mAppViewModelStore;
    }

    public static Application getApplication() {
        return application;
    }

    public static AppContext getSAppContext() {
        if(sAppContext == null){
            throw new NullPointerException("AutoApplication has null AppContext");
        }
        return sAppContext;
    }

    public static AppContext getOriginalSAppContext() {
        return sAppContext;
    }

    public abstract void initDeviceId();

    public abstract void setRequestPhonePermission();

}
